package cz.worktracker.controller;

import cz.worktracker.enums.DataSetsEnum;
import cz.worktracker.model.entities.Project;
import cz.worktracker.model.entities.Task;
import cz.worktracker.model.entities.Taskinterval;
import java.io.Serializable;
import java.util.ArrayList;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import javax.inject.Inject;
import javax.inject.Named;

/**
 * Used for help with actions on recent data.
 * Eg. recent timers, recent projects, etc.
 * @author pagac
 */
@ManagedBean
@SessionScoped
@Named
public class RecentHelperBean extends AbstractManager
                                implements Serializable {

    private static final long serialVersionUID = 1L;

    private DataSetsEnum DSEnum;
    private int maxCountOfRecentTimers = DSEnum.ITEMS_MAX_RECENT_TIMERS.getValue();
    private int counter = 0;
    private boolean next = false;

    @Inject UserManager userManager;
    @Inject ProjectManager projectManager;
    @Inject TaskManager taskManager;
    @Inject TaskintervalManager taskintervalManager;

    private Project project;
    private Task task;
    private Taskinterval taskinterval;

    @PostConstruct
    public void construct() {
        init();
    }

    @PreDestroy
    public void destroy() { }

    private void init() {
        this.project = null;
        this.task = null;
        this.taskinterval = null;


        if( (taskintervalManager.getRecentTasks() != null)
                && (taskintervalManager.getRecentTasks().size() > 0) ) {
            taskinterval = taskintervalManager.getRecentTasks().get( 0 );
            task = taskManager.getTaskById( taskinterval.getIdTask() );
            project = projectManager.getProjectById( task.getIdProject() );
        }
        
        /*
        System.out.println( "TI SIZE: " + taskintervalManager.getRecentTasks().size() );
        System.out.println( "TI : " + task.getName() );
        System.out.println( "PR : " + project.getName() );
         */
    }


    /**
     * Returns a project manager instance
     */
    public Project getProject() {
        return project;
    }


    /**
     * Returns
     */
    public Task getTask() {
        return task;
    }


    /**
     * Returns
     */
    public Taskinterval getTaskinterval() {
        return taskinterval;
    }


    /**
     * Returns
     */
    public void resetCounter() {
        counter = 0;
        next = true;
    }


    /**
     * Returns recent timers.
     * @return
     */
    public boolean getNext() {
        if(taskintervalManager.getTaskIntervalList() == null) {
            init();
        }

        System.out.println( "dataset sice: " + taskintervalManager.getTaskIntervalList().size() );

        if( counter < taskintervalManager.getTaskIntervalList().size() ) {
            this.taskinterval = taskintervalManager.getTaskIntervalList().get( counter );
            this.task = taskManager.getTaskById( taskinterval.getIdTask() );
            this.project = projectManager.getProjectById( task.getIdProject() );

            System.out.println( "counter:" + counter );
            ++counter;
            next = true;
            System.out.println( "next: " + next );

            return true;
        } else {
            counter = 0;
            next = false;

            taskintervalManager.setTaskIntervalList( null );

            return false;
        }
    }

    public boolean isNext() {
        getNext();
        return next;
    }

    public void setNext(boolean next) {
        this.next = next;
    }

}
